//Demo
layui.use(['form', 'layer'], function () {
    var form = layui.form,
        layer = layui.layer;
    var url = "http://120.26.76.247/";

    var order = "";

    //监听提交
    form.on('submit(formDemo)', function (data) {
        layer.msg(JSON.stringify(data.field));
        return false;
    });

    /**
     * 监听下拉框选中之后的值
     */
    form.on('select(filter)', function (data) {
        order = data.value;
    });


    $('#sub_form').on('click', function () {
        var phone = $('#phone').val();
        var name = $('#name').val();
        var gsname = $('#gsname').val();
        var xsname = $('#xsname').val();


        var phone1 = document.getElementById('phone').value;
        if (!(/^1[34578]\d{9}$/.test(phone1))) {
            alert("手机号码有误，请重填");
            return false;
        } else if (name == "") {
            alert("请输入您的称呼");
            return false;
        } else if (gsname == "") {
            alert("请输入您的公司或店铺名字");
            return false;
        } else if (xsname == "") {
            alert("请输入销售顾问姓名");
            return false;
        }
        // if(name == ""){
        //     alert("请输入您的称呼")
        // }

        $.ajax({
            //todo 这边漏掉了一个销售顾问的名字
            url: url + 'api/applySurpriseShop',
            type: 'post',
            data: {
                phone: phone,
                name: name,
                shopName: gsname,
                order: order,
                orderName: xsname
            },
            datatype: 'json',
            success: function (result) {
                var res = JSON.parse(result);
                console.log(res);
                if (res.code == 0) {
                    //成功后的动作
                    alert('提交成功')
                } else {
                    //失败的动作
                    layer.msg(res.msg);
                    return false;
                }
            }
        });
    });

    $.ajax({
        url: url + 'api/getSurpriseMallList',
        type: 'get',
        data: '',
        datatype: 'json',
        success: function (tijiao) {
            var result = JSON.parse(tijiao);
            var data = result.data;
            for (var i = 0; i < data.length; i++) {
                console.log("id:" + data[i].id + "name:" + data[i].name);
                var option = "<option value=\"" + data[i].id + "\"";
                option += ">" + data[i].name + "</option>";  //动态添加数据
                $("select[name=city]").append(option);
            }
            form.render('select');
        }
    });

});
